package ui {
import com.greensock.TweenLite;

import core.AGameObject;

import flash.display.Loader;

import flash.display.MovieClip;
import flash.display.Sprite;
import flash.events.Event;
import flash.events.MouseEvent;
import flash.external.ExternalInterface;

import ru.cjay.loading.UnsafeConnection;
import ru.cjay.loading.ResourceManager;

public class InfoWindowMC extends AGameObject {
    public var window:MovieClip;

    private var _friendCount:int;
    private var _rows:int;
    private var _c_row:int;

    private var _zone_id:int;

    public function InfoWindowMC() {
        window = new (ResourceManager.getInstance().getSwfAssetClass("ui:InfoWindow") as Class)();
        window.caption.width = 330;
        window.caption.multiline = true;
        window.caption.wordWrap = true;
        window.caption.autoSize = "left";
        window.caption.mouseWheelEnabled = false;
        window.info.width = 356;
        window.info.multiline = true;
        window.info.wordWrap = true;
        window.info.autoSize = "left";
        window.info.mouseWheelEnabled = false;

        window.btn_close.addEventListener(MouseEvent.CLICK, onClose);

        window.btn_friend.gotoAndStop(1);
        window.btn_friend.addEventListener(MouseEvent.ROLL_OUT, onButtonRollOut);
        window.btn_friend.addEventListener(MouseEvent.ROLL_OVER, onButtonRollIn);
        window.btn_friend.buttonMode = true;
        window.btn_friend.useHandCursor = true;

        window.btn_friend.visible = false;


        window.friends_panel.scroll_up.addEventListener(MouseEvent.CLICK, function() {
            moveUp();
        });
        window.friends_panel.scroll_down.addEventListener(MouseEvent.CLICK, function() {
            moveDown();
        });
        recalc();
    }

    private function onClose(event:MouseEvent):void {
        dispatchEvent(new Event("event_close_me"));
        if (parent) {
            parent.removeChild(this);
        }
    }

    private function onButtonRollOut(event:MouseEvent):void {
        event.target.gotoAndStop(1);
    }

    private function onButtonRollIn(event:MouseEvent):void {
        event.target.gotoAndStop(2);
    }

    private function finish():void {
        if (window.parent != null) {
            window.parent.removeChild(window);
        }
    }

    public function setCaption(text:String):void {
        window.caption.text = text;
        var myHeight:Number = window.caption.height; //store the height of the textfield
        window.caption.height = myHeight;
        recalc();
    }

    public function setSubCaption(text:String):void {
        window.info.text = text;
        var myHeight:Number = window.info.height; //store the height of the textfield
        window.info.height = myHeight;
        recalc();
    }

    public function recalc():void {
        window.info.y = window.caption.y + window.caption.height;
        window.subjects_lst.y = window.info.y + window.info.height + 5;
        window.friends_panel.y = window.subjects_lst.y + window.subjects_lst.height + 5;
        window.friends_panel.friends_back.height = 145;
        window.btn_friend.y = window.friends_panel.y + 165;
        window.background.height = window.btn_friend.y;
    }

    public function fill_dependencies_list(obj:Object):void {
        var list:Array = new Array();
        var t_obj = new Object();
//        t_obj.text = obj.require_campaign.to_open_hint;
//        t_obj.checked = ;
        list.push(t_obj);
        t_obj = new Array();
        t_obj.text = obj.require_campaign.to_done_hint;
        t_obj.checked = obj.require_campaign.is_done == "false";
        list.push(t_obj);


//        for each (var item in list) {
//            var it = new (ResourceManager.getInstance().getSwfAssetClass("ui:DependenciesLine") as Class)();
        window.subjects_lst.line1.x = 2;
        window.subjects_lst.line2.x = 2;
        window.friends_panel.line3.x = 2;
        window.subjects_lst.line1.text.mouseWheelEnabled = false;
        window.subjects_lst.line1.text.width = 260;
        window.subjects_lst.line1.check.x = 270;
        window.subjects_lst.line1.text.multiline = true;
        window.subjects_lst.line1.text.wordWrap = true;
        window.subjects_lst.line1.text.autoSize = "left";
        window.subjects_lst.line1.text.text = "1. " + obj.require_campaign.to_open_hint;
        window.subjects_lst.line1.check.checked_cp.visible = obj.require_campaign.is_open == "false";

        window.subjects_lst.line2.text.mouseWheelEnabled = false;
        window.subjects_lst.line2.check.x = 270;
        window.subjects_lst.line2.text.width = 260;
        window.subjects_lst.line2.text.multiline = true;
        window.subjects_lst.line2.text.wordWrap = true;
        window.subjects_lst.line2.text.autoSize = "left";
        window.subjects_lst.line2.text.text = "2. " + obj.require_campaign.to_done_hint;
        window.subjects_lst.line2.check.checked_cp.visible = obj.require_campaign.is_done == "false";
        window.subjects_lst.line2.y = window.subjects_lst.line1.y + window.subjects_lst.line1.height;
        window.subjects_lst.back.height = window.subjects_lst.line2.y + window.subjects_lst.line2.height + 10;
        if (window.subjects_lst.back.height < 90) window.subjects_lst.back.height = 90;

        window.friends_panel.y = window.subjects_lst.y + window.subjects_lst.height;

        window.friends_panel.line3.text.mouseWheelEnabled = false;
        window.friends_panel.line3.text.width = 300;
        window.friends_panel.line3.text.multiline = true;
        window.friends_panel.line3.text.wordWrap = true;
        window.friends_panel.line3.text.autoSize = "left";
        window.friends_panel.line3.text.text = "3. Попросить друзей провести разведку";
        window.friends_panel.line3.check.x = 320;
        window.friends_panel.friend_scroll.y = window.friends_panel.line3.y + window.friends_panel.line3.height - 10;
//               window.friends_panel.line3.text.text = "3  Попросить друзей провести разведку";

//        window.friends_panel.friend_scroll.y = window.subjects_lst.y+window.subjects_lst.height + 20;

        setFriendsCount(obj.require_friendshelp);
        var testFriends:Array = obj.friendshelp_slots;

        fillFriends(testFriends);
        recalc();

    }

    public function fillFriends(friends:Array = null):void {

        var pos_x:int = 0;
        var pos_y:int = 0;
        var num:int = 0;
        var isAllFriends:Boolean = true;
        while (window.friends_panel.friend_scroll.numChildren > 0) {
            window.friends_panel.friend_scroll.removeChildAt(0);
        }
        for each(var friend:Object in friends) {
            var icon:MovieClip = new (ResourceManager.getInstance().getSwfAssetClass("ui:FriendIcon") as Class)();

            if (friend.status == "approved") {
                icon.btnAddFriend.visible = false;
                icon.solder_img.visible = false;
                icon.btnBuyFriend.visible = false;
                icon.label.text = friend.fio;
                icon.label.visible = true;
                var icon_loader:Sprite = UnsafeConnection.UnsafeLoader(friend.avatar, 64, 64);
                icon.icon.addChild(icon_loader);
            }
            if (friend.status == "bought") {
                icon.solder_img.visible = true;
                icon.btnAddFriend.visible = false;
                icon.btnBuyFriend.visible = false;
                icon.label.text = "Куплен";
                icon.label.visible = true;
            }
            if (friend.status == "none") {
                isAllFriends = false;
                icon.solder_img.visible = false;
                icon.btnAddFriend.visible = true;
                icon.btnBuyFriend.visible = true;

//                icon.label.text = friend.fio;
                icon.label.visible = false;

                icon.btnBuyFriend.label.y = -17;
                icon.btnBuyFriend.label.width=50;
                icon.btnBuyFriend.label.multiline = true;
                icon.btnBuyFriend.label.wordWrap = true;
                icon.btnBuyFriend.label.autoSize = "left";
                icon.btnBuyFriend.label.mouseWheelEnabled = false;

                icon.btnBuyFriend.label.text = friend.buy_hint;
                icon.btnBuyFriend.gotoAndStop(1);
                (icon.btnBuyFriend as MovieClip).mouseChildren = false;
                (icon.btnBuyFriend as MovieClip).mouseEnabled = true;
                (icon.btnBuyFriend as MovieClip).buttonMode = true;
                icon.btnBuyFriend.back.height = icon.btnBuyFriend.label.height;

                icon.btnBuyFriend.addEventListener(MouseEvent.ROLL_OVER,function(event:MouseEvent){
                    (event.currentTarget as MovieClip).gotoAndStop(2);
                });
                icon.btnBuyFriend.addEventListener(MouseEvent.ROLL_OUT,function(event:MouseEvent){
                    (event.currentTarget as MovieClip).gotoAndStop(1);
                });

                icon.btnAddFriend.addEventListener(MouseEvent.CLICK, function() {
                    trace("WHERE IS MY FRIEND?");
                    ExternalInterface.call("Application.friendRequests.friendsList", friend.request_id, _zone_id)
                    dispatchEvent(new Event("event_close_me"));
                    if (parent) {
                        parent.removeChild(this);
                    }
                })
                icon.btnBuyFriend.addEventListener(MouseEvent.CLICK, function() {
                    trace("HERE IS MY MONEY");
//                    ExternalInterface.call("Application.friendRequests.buyFriendRequest",1,_zone_id,callback)
                    dispatchEvent(new Event("event_close_me"));
                    if (parent) {
                        parent.removeChild(this);
                    }
                })
            }
            if (pos_x > 4) {
                pos_x = 0;
                pos_y += 1;
            }
            icon.x = pos_x * 60;
            icon.y = pos_y * 100;
            window.friends_panel.friend_scroll.addChild(icon);
            pos_x += 1;
            num += 1;
        }
        window.friends_panel.line3.check.checked_cp.visible = isAllFriends;
        window.friends_panel.friend_scroll.y = _c_row * 100 - 43;
    }


    public function setFriendsCount(p_count:int):void {
        _friendCount = p_count;
        _rows = p_count / 5;
        _c_row = 1;
    }


    public function moveUp():void {
        _c_row += 1;
        if (_c_row > _rows) {
            _c_row = _rows;
        } else {
            TweenLite.to(window.friends_panel.friend_scroll, 0.25, { y:_c_row * 100 - 43 });
        }
    }

    public function moveDown():void {
        _c_row -= 1;
        if (_c_row < 0) {
            _c_row = 0;

        } else {
            TweenLite.to(window.friends_panel.friend_scroll, 0.25, { y:_c_row * 100 - 43 });
        }
    }

    public function get c_row():int {
        return _c_row;
    }

    public function get rows():int {
        return _rows;
    }

    public function get zone_id():int {
        return _zone_id;
    }

    public function set zone_id(value:int):void {
        _zone_id = value;
    }
}
}
